﻿using System;
using System.Reflection;

namespace Backend.JYD.Common
{
    /// <summary>
    /// 反射代码类
    /// </summary>
    public class ReflectionHelper
    {
        /// <summary>
        /// 读取对象属性
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static System.Reflection.PropertyInfo[] GetProperties(object obj)
        {
            Type type = obj.GetType();
            PropertyInfo[] propertyInfo = type.GetProperties();
            return propertyInfo;
        }

        /// <summary>
        /// 读取对象属性
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static System.Reflection.PropertyInfo[] GetProperties(Type type)
        {
            PropertyInfo[] propertyInfo = type.GetProperties();
            return propertyInfo;
        }

        /// <summary>
        /// 根据属性名得到对象属性值
        /// </summary>
        /// <param name="PropertyName"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object GetPropertyValue(string PropertyName, object obj)
        {
            Type type = obj.GetType();
            return type.GetProperty(PropertyName).GetValue(obj, null);
        }

        /// <summary>
        /// 根据属性名得到对象属性值
        /// </summary>
        /// <param name="PropertyName"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object GetPropertyValueToNull(string PropertyName, object obj)
        {
            Type type = obj.GetType();
            object _object = type.GetProperty(PropertyName).GetValue(obj, null);
            if (_object == null)
                return "";
            else
                return _object;
        }

        /// <summary>
        /// 根据属性名赋属性值
        /// </summary>
        /// <param name="PropertyName"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object SetPropertyValue(object obj, string PropertyName, object value)
        {
            Type type = obj.GetType();
            type.GetProperty(PropertyName).SetValue(obj, value, null);
            return obj;
        }

        /// <summary>
        /// 根据属性名期于类型赋值
        /// </summary>
        /// <param name="obj">赋值的实体对象</param>
        /// <param name="propName">属性名</param>
        /// <param name="propValue">属性值</param>
        /// <returns></returns>
        public static object SetTypedPropertyValue(object obj, string propName, object propValue)
        {
            if (propValue != null)
            {
                object propTypedValue = new object();
                Type type = obj.GetType();
                Type propertyType = type.GetProperty(propName).PropertyType;

                if (propertyType.Equals(typeof(string)))
                {
                    propTypedValue = propValue;
                }
                else if (propertyType.Equals(typeof(Int16)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString()) || !NumberHelper.IsNumeric(propValue.ToString()))
                        propValue = 0;

                    propTypedValue = Convert.ToInt16(propValue);
                }
                else if (propertyType.Equals(typeof(Int16?)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString()) || !NumberHelper.IsNumeric(propValue.ToString()))
                        return obj;

                    propTypedValue = Convert.ToInt16(propValue);
                }
                else if (propertyType.Equals(typeof(Int32)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString()) || !NumberHelper.IsNumeric(propValue.ToString()))
                        propValue = 0;

                    propTypedValue = Convert.ToInt32(propValue);
                }
                else if (propertyType.Equals(typeof(Int32?)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString()) || !NumberHelper.IsNumeric(propValue.ToString()))
                        return obj;

                    propTypedValue = Convert.ToInt32(propValue);
                }
                else if (propertyType.Equals(typeof(long)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString()) || !NumberHelper.IsNumeric(propValue.ToString()))
                        propValue = 0;

                    propTypedValue = Convert.ToInt64(propValue);
                }
                else if (propertyType.Equals(typeof(long?)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString()) || !NumberHelper.IsNumeric(propValue.ToString()))
                        return obj;

                    propTypedValue = Convert.ToInt64(propValue);
                }
                else if (propertyType.Equals(typeof(DateTime)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString())) propValue = DateTime.Today.ToShortDateString();

                    propTypedValue = Convert.ToDateTime(propValue);
                }
                else if (propertyType.Equals(typeof(DateTime?)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString())) return obj;

                    propTypedValue = Convert.ToDateTime(propValue);
                }
                else if (propertyType.Equals(typeof(decimal)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString()) || !NumberHelper.IsNumeric(propValue.ToString()))
                        propValue = 0;

                    propTypedValue = Convert.ToDecimal(propValue);
                }
                else if (propertyType.Equals(typeof(decimal?)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString()) || !NumberHelper.IsNumeric(propValue.ToString()))
                        return obj;

                    propTypedValue = Convert.ToDecimal(propValue);
                }
                else if (propertyType.Equals(typeof(double)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString()) || !NumberHelper.IsNumeric(propValue.ToString()))
                        propValue = 0;

                    propTypedValue = Convert.ToDouble(propValue);
                }
                else if (propertyType.Equals(typeof(double?)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString()) || !NumberHelper.IsNumeric(propValue.ToString()))
                        return obj;

                    propTypedValue = Convert.ToDouble(propValue);
                }
                else if (propertyType.Equals(typeof(bool)))
                {
                    propTypedValue = Convert.ToBoolean(propValue);
                }
                else if (propertyType.Equals(typeof(bool?)))
                {
                    if (string.IsNullOrEmpty(propValue.ToString())) return obj;

                    propTypedValue = Convert.ToBoolean(propValue);
                }
                else
                    propTypedValue = propValue;

                SetPropertyValue(obj, propName, propTypedValue);
            }
            return obj;
        }

    }
}
